package ${package}.service.impl;

import java.util.List;
import ${package}.ao.QueryInfo;
import ${package}.ao.ResultSet;
import ${package}.po.${table.className};
import ${package}.mapper.${table.className}Mapper;
import ${package}.service.${table.className}Service;
import org.springframework.util.Assert;
import java.util.Arrays;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import ${package}.po.${table.className}Example;
import ${package}.ao.DataStatus;

@Service
public class ${table.className}ServiceImpl implements ${table.className}Service{
	
	@Autowired
	private ${table.className}Mapper ${table.lowerClassName}Mapper;
	
	@Override
	public List<${table.className}> find() {
		${table.className}Example example = new ${table.className}Example();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ${table.lowerClassName}Mapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		${table.className}Example example = new ${table.className}Example();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ${table.lowerClassName}Mapper.countByExample(example);
	}

	@Override
	public ${table.className} findById(Long id) {
		return ${table.lowerClassName}Mapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(${table.className} ${table.lowerClassName}) {
		return ${table.lowerClassName}Mapper.insertSelective(${table.lowerClassName});
	}

	@Override
	public int update(${table.className} ${table.lowerClassName}) {
		return ${table.lowerClassName}Mapper.updateByPrimaryKeySelective(${table.lowerClassName});
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		${table.className}Example example = new ${table.className}Example();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		${table.className} record = new ${table.className}();
		record.setStatus(DataStatus.Delete);
		return ${table.lowerClassName}Mapper.updateByExampleSelective(record, example);
	}
	
	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[]{id});
	}

	@Override
	public int saveOrUpdate(${table.className} ${table.lowerClassName}) {
		int r;
		if (${table.lowerClassName}.getId() != null) {
			r = this.update(${table.lowerClassName});
		} else {
			r = this.save(${table.lowerClassName});
		}
		return r;
	}

	@Override
	public ResultSet<${table.className}> search(QueryInfo queryInfo, String q) {
		int count = ${table.lowerClassName}Mapper.countSuper(q);
		if (count == 0) {
			return new ResultSet<>();
		}
		List<${table.className}> list = ${table.lowerClassName}Mapper.selectSuper(queryInfo, q);
		return new ResultSet<>(count,list);
	}

}

